package com.qiantu.phone.ui.dialog;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.qiantu.api.entity.UserPushSetsBean;
import com.qiantu.phone.R;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:    终端设备列表
 */

public class BottomTerminalsDialog extends BottomPopupView {
    private RecyclerView rv_terminals;
    private DialogClickListener mDialogClickListener;
    private List<UserPushSetsBean.UserPushSets.Terminals> terminalslist;    //原数据
    private List<UserPushSetsBean.UserPushSets.Terminals> mTerminals = new ArrayList<>();   //选中数据
    private Map<Integer, Boolean> checkBoxMap = new HashMap<>();

    public BottomTerminalsDialog(@NonNull Context context,List<UserPushSetsBean.UserPushSets.Terminals> terminals) {
        super(context);
        terminalslist = terminals;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_terminals;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        rv_terminals = findViewById(R.id.rv_terminals);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDialogClickListener != null) {
                    mDialogClickListener.onConfirmClick(mTerminals);
                }
            }
        });
        initadapter();
    }

    private void initadapter() {
        for (int i = 0; i < terminalslist.size(); i++) {
            if (terminalslist.get(i).isPush()){
                checkBoxMap.put(i,true);
                mTerminals.add(terminalslist.get(i));
            }else {
                checkBoxMap.put(i,false);
            }
        }
        TerMinalsAdapter terMinalsAdapter = new TerMinalsAdapter(terminalslist);
        rv_terminals.setAdapter(terMinalsAdapter);
        rv_terminals.setLayoutManager(new LinearLayoutManager(getContext()));
    }




    private class TerMinalsAdapter extends BaseQuickAdapter<UserPushSetsBean.UserPushSets.Terminals, BaseViewHolder>{

        public TerMinalsAdapter(@Nullable List<UserPushSetsBean.UserPushSets.Terminals> data) {
            super(R.layout.item_terminals, data);
        }

        @Override
        public void onBindViewHolder(@NonNull BaseViewHolder holder, @SuppressLint("RecyclerView") int position) {
            super.onBindViewHolder(holder, position);
            CheckBox view = holder.getView(R.id.check);
            view.setOnCheckedChangeListener(null);
            view.setChecked(checkBoxMap.get(position));
            view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkBoxMap.put(position,isChecked);
                }
            });
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, UserPushSetsBean.UserPushSets.Terminals item) {
            holder.setText(R.id.terminal_name,item.getTerminalName());
            CheckBox check = holder.getView(R.id.check);
            check.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (check.isChecked()){
                        item.setPush(true);
                        mTerminals.add(item);
                    }else {
                        item.setPush(false);
                        mTerminals.remove(item);
                    }
                }
            });
        }
    }


    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(List<UserPushSetsBean.UserPushSets.Terminals> terminals);
    }
}
